﻿using MongdbHelper;
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using UIModel.Mongo;

namespace MongoTest
{

    /// <summary>
    /// 同步mongo数据
    /// </summary>
    public class SynMongoData
    {
        private string connstr = "mongodb://admin:adminlinux@52.83.104.122:30000,52.83.216.230:30000/";
        public void Job()
        {

            try
            {
                //var momgo2 = new MongoDBHelper("mongodb://wpaysys:bj8ploe3chy@52.83.198.146:27017", "WPMDB", "VipMthMongo");//旧数据库
                var momgo2 = new MongoDBHelper("mongodb://localhost:27019", "Staff", "SaleMongo");//旧数据库
                //int outTotalPage; int totalCount;
                //var tist = momgo2.GetAll<Staff>(x => true);
                //var tist2 = momgo2.GetPageList<WriteModel<CacheGft>>(1, 1000, out outTotalPage, out totalCount, x => true, x => x.ModelType).ToList();
                var list = new System.Collections.Generic.List<WriteModel<Staff>>();
                var list2 = new System.Collections.Generic.List<Staff>();
                int count = 0;
                //foreach (var iitem in tist)
                //{
                //    count++;
                //    list.Add(new InsertOneModel<Staff>(iitem));
                //}

                Stopwatch s = new Stopwatch();
                s.Start();
                for (int i = 0; i < 10000000; i++)
                {
                    list.Add(new InsertOneModel<Staff>(new Staff
                    {
                        Name = i + "staff",
                        Age = i,
                        Bir = DateTime.Now
                    }));
                }
                s.Stop();
                Console.WriteLine($"循环时间,耗时：" + s.ElapsedMilliseconds);
                s = new Stopwatch();
                s.Start();
                momgo2 = new MongoDBHelper("mongodb://localhost:27019", "Staff", "BulkWrite");
                momgo2.BulkWrite<Staff>(list);
                s.Stop();
                Console.WriteLine($"BulkWrite写入中完成,耗时：" + s.ElapsedMilliseconds);
                Console.WriteLine("==================分割线==================");
                s = new Stopwatch();
                s.Start();
                for (int i = 0; i < 10000000; i++)
                {
                    list2.Add(new Staff
                    {
                        Name = i + "staff",
                        Age = i,
                        Bir = DateTime.Now
                    });
                }
                s.Stop();
                Console.WriteLine($"循环时间,耗时：" + s.ElapsedMilliseconds);

                s = new Stopwatch();
                s.Start();
                momgo2 = new MongoDBHelper("mongodb://localhost:27019", "Staff", "InsertMany");
                // momgo2.InsertMany<Staff>(list2);
                s.Stop();
                Console.WriteLine($"InsertMany写入中完成,耗时：" + s.ElapsedMilliseconds);




            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        public void DanciTaskJob(int count)
        {
            try
            {
                Console.WriteLine($"==================递增并发插入：{count}============================");
                var momgo = new MongoDBHelper("mongodb://wpaysys:bj8ploe3chy@52.83.198.146:27017", "WPMDB", "SaleDtlMongo");//旧数据库读取
                var list2 = momgo.GetAll2<SaleDtlMongo>(x => true, count);
                Stopwatch s = new Stopwatch();

                momgo = new MongoDBHelper(connstr, "WPMDBTest", "SaleDtlMongoInt");
                var indexth = 0;
                for (int j = 0; j < 10; j++)
                {
                    List<SaleDtlMongo> list = new List<SaleDtlMongo>();
                    int index = 1;

                    foreach (var iitem in list2)
                    {
                        iitem.dz = index;
                        iitem._id = ObjectId.GenerateNewId();
                        index++;
                        list.Add(iitem);
                    }
                    var list3 = list.OrderByDescending(x => x.SalDt).ToList();
                    Task.Run(() =>
                    {

                        s = new Stopwatch();
                        s.Start();
                        momgo.InsertMany<SaleDtlMongo>(list3);
                        s.Stop();
                        var str = $"递增单次写入完成{ s.ElapsedMilliseconds}\n";
                        Console.WriteLine(str);
                        LoggerHelper.Log(str);

                    });
                    indexth++;
                }
            }
            catch (Exception e)
            {

                Console.WriteLine(e);
            }
        }
        public void DanciTaskJob2(int count)
        {
            Console.WriteLine($"==================哈希并发插入：{count}============================");
            try
            {

                var momgo = new MongoDBHelper("mongodb://wpaysys:bj8ploe3chy@52.83.198.146:27017", "WPMDB", "SaleDtlMongo");//旧数据库读取
                var list2 = momgo.GetAll2<SaleDtlMongo>(x => true, count);
                Stopwatch s = new Stopwatch();
                momgo = new MongoDBHelper(connstr, "WPMDBTest", "SaleDtlMongohashed");
                var indexth = 0;
                for (int j = 0; j < 10; j++)
                {
                    List<SaleDtlMongo> list = new List<SaleDtlMongo>();
                    int index = 1;
                    foreach (var iitem in list2)
                    {
                        iitem.dz = index;
                        iitem._id = ObjectId.GenerateNewId();
                        index++;
                        list.Add(iitem);
                    }
                    var list3 = list.OrderByDescending(x => x.SalDt).ToList();
                    Task.Run(() =>
                    {
                        s = new Stopwatch();
                        s.Start();
                        momgo.InsertMany<SaleDtlMongo>(list3);
                        s.Stop();
                        var str = $"哈希单次写入完成{ s.ElapsedMilliseconds}\n";
                        Console.WriteLine(str);
                        LoggerHelper.Log(str);

                    });
                    indexth++;
                }
            }
            catch (Exception e)
            {

                Console.WriteLine(e);
            }
        }

        public void InsertJob1(int count)
        {

            Console.WriteLine($"==================递增单次插入：{count}============================");
            var momgo = new MongoDBHelper("mongodb://wpaysys:bj8ploe3chy@52.83.198.146:27017", "WPMDB", "SaleDtlMongo");//旧数据库读取
            var list = momgo.GetAll2<SaleDtlMongo>(x => true, count);
            Stopwatch s = new Stopwatch();
            var listmany = new List<SaleDtlMongo>();
            int index = 1;
            foreach (var iitem in list)
            {
                iitem.dz = index;
                iitem._id = ObjectId.GenerateNewId();
                index++;
                listmany.Add(iitem);
            }
            var list3 = listmany.OrderByDescending(x => x.SalDt).ToList();
            momgo = new MongoDBHelper(connstr, "WPMDBTest", "SaleDtlMongoInt");
            s.Start();
            momgo.InsertMany<SaleDtlMongo>(list3);
            s.Stop();
            var str = $"递增写入完成{ s.ElapsedMilliseconds}\n";
            Console.WriteLine(str);
            LoggerHelper.Log(str);
        }
        public void InsertJob2(int count)
        {
            Console.WriteLine($"==================哈希单次插入：{count}============================");
            var momgo = new MongoDBHelper("mongodb://wpaysys:bj8ploe3chy@52.83.198.146:27017", "WPMDB", "SaleDtlMongo");//旧数据库读取
            var list = momgo.GetAll2<SaleDtlMongo>(x => true, count);
            Stopwatch s = new Stopwatch();
            var listmany = new List<SaleDtlMongo>();
            int index = 1;
            foreach (var iitem in list)
            {
                iitem.dz = index;
                iitem._id = ObjectId.GenerateNewId();
                index++;
                listmany.Add(iitem);
            }
            var list3 = listmany.OrderByDescending(x => x.SalDt).ToList();
            momgo = new MongoDBHelper(connstr, "WPMDBTest", "SaleDtlMongohashed");
            s.Start();
            momgo.InsertMany<SaleDtlMongo>(list3);
            s.Stop();
            var str = $"哈希写入完成{ s.ElapsedMilliseconds}\n";
            Console.WriteLine(str);
            LoggerHelper.Log(str);
        }

        public void FindJob(int count, DateTime date)
        {
            try
            {
                Stopwatch s = new Stopwatch();
                Console.WriteLine($"---------------------------按片建查询{date}---------------------------\n");
                var momgo = new MongoDBHelper(connstr, "WPMDBTest", "SaleDtlMongoInt");
                s = new Stopwatch();
                s.Start();
                var list = momgo.GetAllFind<SaleDtlMongo>(count, x => x.SalDt == date);
                s.Stop();
                var str = $"递增查询完成片建条件{ s.ElapsedMilliseconds}\n";
                Console.WriteLine(str);
                LoggerHelper.Log(str);

            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
        public void FindJob2(int count, DateTime date)
        {
            try
            {
                Stopwatch s = new Stopwatch();
                Console.WriteLine($"---------------------------按片建查询{date}---------------------------\n");
                var momgo = new MongoDBHelper(connstr, "WPMDBTest", "SaleDtlMongohashed");
                s = new Stopwatch();
                s.Start();
                var list = momgo.GetAllFind<SaleDtlMongo>(count, x => x.SalDt == date);
                s.Stop();
                var str = $"哈希查询完成片建条件{ s.ElapsedMilliseconds}\n";
                Console.WriteLine(str);
                LoggerHelper.Log(str);

            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
        public void FindJob3(int count)
        {
            try
            {
                Stopwatch s = new Stopwatch();
             
                var momgo = new MongoDBHelper(connstr, "WPMDBTest", "SaleDtlMongoInt");
                s = new Stopwatch();
                s.Start();
                var list = momgo.GetAllFind<SaleDtlMongo>(count, x => x.SllTyp == 1);
                s.Stop();
                var str = $"递增查询完成非片建条件{ s.ElapsedMilliseconds}\n";
                Console.WriteLine(str);
                LoggerHelper.Log(str);

            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
        public void FindJob4(int count)
        {
            try
            {
                Stopwatch s = new Stopwatch();
              
                var momgo = new MongoDBHelper(connstr, "WPMDBTest", "SaleDtlMongohashed");
                s = new Stopwatch();
                s.Start();
                var list = momgo.GetAllFind<SaleDtlMongo>(count, x => x.SllTyp == 1);
                s.Stop();
                var str = $"哈希查询完成非片建条件{ s.ElapsedMilliseconds}\n";
                Console.WriteLine(str);
                LoggerHelper.Log(str);

            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }

    }
}
